﻿﻿<%--
SYSTEM NAME 	: 기술적보호조치표준서비스
PROGRAM ID 		: testPlanRegSave
PROGRAM NAME 	: 시나리오 등록 처리
AUTHOR 			: 신창권
CREAT DATE 		: 2010-11-13
MODIFY DATE		: 
DESCRIPTION 	: 
--%>
<%@ page language="java"
	import="java.sql.*,javax.sql.*,java.util.*,java.io.*,javax.naming.*"
	pageEncoding="UTF-8"%>
<%@ page import="com.markany.beans.util.*"%>
<%@ page import="com.markany.beans.property.*"%>
<%@ page import="com.markany.beans.src.MasterDao"%>
<%@ page import="org.apache.log4j.Logger"%>
<%! private Logger logger = Logger.getLogger(this.getClass().getName()); %>
<%
	//서버패스 잡기
	String ctxPath = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + ctxPath + "/ctis_mng";
	
	String PAGE = "";
	String DIR = "";
	String PAGE_NAME = "";
	String NAV = "";
%>
<%@ include file="/jsp/common/ctis_sessinc.jsp" %>
<%
	String test_plan_id = hp.getString("test_plan_id","");
	String test_plan_nm = hp.getString("test_plan_nm","");
	String audio_remove_rate = hp.getString("audio_remove_rate","");	
	String[] audio_chk = hp.getArrayString("audio_chk","");
	String[] video_chk = hp.getArrayString("video_chk","");
	String[] image_chk = hp.getArrayString("image_chk","");
	String[] audio_test_type_cnt = hp.getArrayString("A_test_type_cnt","");
	String[] video_test_type_cnt = hp.getArrayString("V_test_type_cnt","");
	String[] image_test_type_cnt = hp.getArrayString("I_test_type_cnt","");
	String[] audio_test_type_cd = hp.getArrayString("A_test_type_cd","");
	String[] video_test_type_cd = hp.getArrayString("V_test_type_cd","");
	String[] image_test_type_cd = hp.getArrayString("I_test_type_cd","");
	
	if (audio_remove_rate.equals("")) audio_remove_rate = "0";
	
	
	int arrPos = 0;
	MasterDao dao = null;
	String errorMsg = "";
	String sql = "";

	Vector list = null;
	Vector list2 = null;
	Vector list3 = null;
	Vector genList = null;
	
	try{
		// 시나리오 등록시 (시나리오 ID값이 없음)
		if (test_plan_id.equals("")){
			Calendar cal = Calendar.getInstance();
			String year = String.valueOf(cal.get(Calendar.YEAR));

			// 최대 값 구하기
			dao = new MasterDao();
			sql = "SELECT NVL(MAX(SUBSTR(TEST_PLAN_ID, 5, 3)), 0) + 1 TEST_PLAN_ID FROM TAB_TEST_2_PLAN WHERE TEST_PLAN_ID >= '" + year + "000' AND TEST_PLAN_ID <= '" + year + "999' ";
			dao.setCommand(sql);
			list = dao.getResultHashtableNoClose();
			if (list.size() > 0)
			{
				test_plan_id = ((Hashtable) list.elementAt(0)).get("TEST_PLAN_ID")+"";
				if (test_plan_id.length() == 1)
					test_plan_id = "00" + test_plan_id;
				else if (test_plan_id.length() == 2)
					test_plan_id = "0" + test_plan_id;
				test_plan_id = year + test_plan_id;
			}
			else
			{
				test_plan_id = year + "001";
			}
			dao.close();
			
			dao = new MasterDao();
			// 신규 ID 생성
			sql = "INSERT INTO TAB_TEST_2_PLAN@TEST_DB_LINK (TEST_PLAN_ID, TEST_PLAN_NM, TEST_GB, USE_YN, AUDIO_REMOVE_RATE, REG_DATE) VALUES ('" + test_plan_id + "', '" + test_plan_nm + "', 'FF', 'Y', '" + audio_remove_rate + "', SYSDATE)";
			dao.execute(sql); 
			dao.close();
		}
		
		for (int loop = 0; loop<=2; loop++)
		{
			String cont_gb = "";
			String cont_name = "";
			
			if (loop == 0){
				cont_name = "AUDIO";
			}else if (loop == 1){
				cont_name = "VIDEO";
			}else if (loop == 2){
				cont_name = "IMAGE"; 
			}
			cont_gb = cont_name.substring(0, 1); // A , V , I 

			String[] arr_test_type_cd = hp.getArrayString(cont_gb + "_test_type_cd",""); // 평가항목
			String[] arr_test_type_cnt = hp.getArrayString(cont_gb + "_test_type_cnt",""); // 대상수량

			// 강인성 장르별 분류 검사
			//X310: 강인성, GA00, GV00, GI00: 장르, CONT_TYPE이 2면 변형, NO_DB_CONT_YN가 N이면 무디비가 아님
			sql = "SELECT GENRE, COUNT(*) CNT, ";
			sql += "	ROUND( COUNT(*) /  ";
			sql += "	(SELECT COUNT(*)  ";
			sql += "    	FROM PES_GUI_INFO A ";
			sql += "        INNER JOIN TAB_CODE ON TAB_CODE.B_CD = 'G" + cont_gb + "00' AND TAB_CODE.CD = A.GENRE "; 
			sql += "		WHERE A.CONT_TYPE = '2' AND A.TEST_TYPE_CD = 'X310' ";
			sql += "			AND NO_DB_CONT_YN = 'N'), 2) CUR_PERCENT ";  
			sql += "FROM PES_GUI_INFO A ";
			sql += "INNER JOIN TAB_CODE ON TAB_CODE.B_CD = 'G" + cont_gb + "00' AND TAB_CODE.CD = A.GENRE AND TAB_CODE.USE_YN = '1' "; 
			sql += "WHERE A.CONT_TYPE = '2' AND TEST_TYPE_CD = 'X310' AND A.USE_YN = 'Y' ";
			sql += "GROUP BY GENRE ";
			sql += "ORDER BY COUNT(*) ASC ";
			dao = new MasterDao();
			if (LOG_MODE) System.out.print(sql);
			dao.setCommand(sql);
			genList = dao.getResultHashtableNoClose();
			dao.close();
						
			String test_type_cd = "";
			String test_type_cnt = "";
			String robust_param = "";
			
			for (int i=0; i<arr_test_type_cd.length; i++) 
			{
				test_type_cd = arr_test_type_cd[i];  //arr_test_type_cd 평가항목
				test_type_cnt = arr_test_type_cnt[i]; //arr_test_type_cnt 대상수량
				
				// 신규등록이 아닐경우 기존자료 삭제
				// 수정일 경우도 삭제하고 다시 등록하는 방식.
				if (!test_plan_id.equals(""))
				{
					dao = new MasterDao();
					// 기존 자료 삭제
					sql = "DELETE FROM TAB_TEST_2_PLAN_CONTENTS WHERE TEST_PLAN_ID = '" + test_plan_id + "' AND CONT_GB = '" + cont_gb + "' AND TEST_TYPE_CD = '" + test_type_cd + "' ";
					dao.execute(sql);
					dao.close();
				}

				
				if (test_type_cd.equals("X340")){ //특징정보량
					
					int selectCnt2 = 0;
					int genreInsertCnt = 0;
					// 장르별 분포를 고르게 하기
					
					for (int l=0; l<genList.size(); l++){
						selectCnt2 = 0;
						float cur_persent = 0;
						String genre = ((Hashtable) genList.elementAt(l)).get("GENRE")+"";
						
						// genre = ((Hashtable) genList.elementAt(l)).get("GENRE")+"";
						// cur_persent = Float.parseFloat(((Hashtable) genList.elementAt(l)).get("CUR_PERCENT")+"");
						selectCnt2 = (int) (Integer.parseInt(test_type_cnt) / genList.size());
						if (selectCnt2 == 0) selectCnt2 = 1;
						
						genreInsertCnt += selectCnt2;
						
						dao = new MasterDao();
						sql = "INSERT INTO TAB_TEST_2_PLAN_CONTENTS (TEST_PLAN_ID, CONT_GB, TEST_TYPE_CD, CONTENT_ID, REG_DATE) ";
						sql += "SELECT * FROM (SELECT '" + test_plan_id + "', '" + cont_gb + "', 'X340', CONTENT_ID, SYSDATE FROM ";
						sql += "	(SELECT ROWNUM, CONTENT_ID FROM PES_GUI_INFO WHERE CONT_TYPE = '0' AND USE_YN = 'Y' AND GENRE = '" + genre + "') TEMP";
						sql += "	ORDER BY DBMS_RANDOM.VALUE() ) LIST ";
						sql += "WHERE ROWNUM <= " + String.valueOf(selectCnt2);
	
						if (LOG_MODE) System.out.print(sql);
						dao.execute(sql); 
						dao.close();
						
						if (genreInsertCnt > Integer.parseInt(test_type_cnt)) break;
					}
					
					// 전체 대상 수량보다 Insert된 수량이 적은 경우 램덤하게 해서 수량 채우기
					// ???
					sql = "SELECT COUNT(*) CNT FROM TAB_TEST_2_PLAN_CONTENTS WHERE TEST_PLAN_ID = '" + test_plan_id + "' AND CONT_GB = '" + cont_gb + "' AND TEST_TYPE_CD = 'X340'";
					dao = new MasterDao();
					if (LOG_MODE) System.out.print(sql);
					dao.setCommand(sql);
					list = dao.getResultHashtableNoClose();
					dao.close();
					
					int insertedCnt = Integer.parseInt(((Hashtable) list.elementAt(0)).get("CNT")+"");
					if (insertedCnt < Integer.parseInt(test_type_cnt)){
						sql = "INSERT INTO TAB_TEST_2_PLAN_CONTENTS (TEST_PLAN_ID, CONT_GB, TEST_TYPE_CD, CONTENT_ID, REG_DATE) ";
						sql += "SELECT * FROM (SELECT '" + test_plan_id + "', '" + cont_gb + "', 'X340', CONTENT_ID, SYSDATE FROM ";
						sql += "	(SELECT ROWNUM, CONTENT_ID FROM PES_GUI_INFO WHERE CONT_TYPE = '0' AND USE_YN = 'Y' ";
						sql += "    AND CONTENT_ID NOT IN (SELECT CONTENT_ID FROM TAB_TEST_2_PLAN_CONTENTS ";
						sql += "    	WHERE TEST_PLAN_ID = '" + test_plan_id + "' AND CONT_GB = '" + cont_gb + "' AND TEST_TYPE_CD = 'X340') ) TEMP";
						sql += "	ORDER BY DBMS_RANDOM.VALUE() ) LIST ";
						sql += "WHERE ROWNUM <= " + String.valueOf(Integer.parseInt(test_type_cnt) - insertedCnt);
						dao = new MasterDao();
						if (LOG_MODE) System.out.print(sql);
						dao.execute(sql); 
						dao.close();
					}	
				}else if (test_type_cd.equals("X380")){ // 부분 매칭
				
					// 2011/10/26 2개혼합, 3개 혼합이므로 구분하여 가져오기
					/*
					int subset2Cnt = Integer.parseInt(test_type_cnt) / 2;
					int subset3Cnt = Integer.parseInt(test_type_cnt) - subset2Cnt;
					*/
					
					// 2개 혼합 콘텐츠
					/*
					dao = new MasterDao();
					sql = "INSERT INTO TAB_TEST_2_PLAN_CONTENTS (TEST_PLAN_ID, CONT_GB, TEST_TYPE_CD, CONTENT_ID, REG_DATE) ";
					sql += "SELECT '" + test_plan_id + "', '" + cont_gb + "', '" + test_type_cd + "', CONTENT_ID, SYSDATE FROM ";
					sql += "	(SELECT ROWNUM, PES_GUI_INFO.CONTENT_ID FROM PES_GUI_INFO ";
					sql += "        INNER JOIN PES_GUI_SUBSET ON PES_GUI_INFO.CONTENT_ID = PES_GUI_SUBSET.CONTENT_ID ";
					sql += "        WHERE CONT_TYPE = '2' AND TEST_TYPE_CD = 'X380' AND USE_YN = 'Y' AND ORG_CONT_ID3 IS NULL ";
					sql += "	ORDER BY DBMS_RANDOM.VALUE() ) LIST ";
					sql += "WHERE ROWNUM <= " + subset2Cnt;
					dao = new MasterDao();
					if (LOG_MODE) System.out.print(sql);
					dao.execute(sql); 
					dao.close();
					*/
					
					// 3개 혼합 콘텐츠
					dao = new MasterDao();
					if(LOG_MODE){
						System.out.println("부분매칭 저장!!");
					}
					sql = "INSERT INTO TAB_TEST_2_PLAN_CONTENTS (TEST_PLAN_ID, CONT_GB, TEST_TYPE_CD, CONTENT_ID, REG_DATE) ";
					sql += "SELECT '" + test_plan_id + "', '" + cont_gb + "', '" + test_type_cd + "', CONTENT_ID, SYSDATE FROM ";
					sql += "	(SELECT ROWNUM, PES_GUI_INFO.CONTENT_ID FROM PES_GUI_INFO ";
					sql += "        INNER JOIN PES_GUI_SUBSET ON PES_GUI_INFO.CONTENT_ID = PES_GUI_SUBSET.CONTENT_ID ";
					sql += "        WHERE CONT_TYPE = '2' AND TEST_TYPE_CD = 'X380' AND USE_YN = 'Y' AND ORG_CONT_ID3 IS NOT NULL ";
					sql += "	ORDER BY DBMS_RANDOM.VALUE() ) LIST ";
					sql += "WHERE ROWNUM <= " + test_type_cnt;
					dao = new MasterDao();
					if (LOG_MODE) System.out.print(sql);
					dao.execute(sql); 
					dao.close();
				}else if ((test_type_cd.equals("X310")) || (test_type_cd.equals("X370"))){
					// 해당 평가항목내에 변형파라메터가 몇개까지 있는지 검사.
					// (이전쿼리) sql = "SELECT SUBSTR(PARAM_CD, 1, 3) ROBUST, COUNT(*) CNT FROM TAB_TEST_2_ITEM WHERE CONT_GB = 'A' GROUP BY SUBSTR(PARAM_CD, 1, 3) ORDER BY SUBSTR(PARAM_CD, 1, 3)";
					sql = "SELECT SUBSTR(CD, 1, 3) ROBUST, COUNT(*) CNT FROM TAB_CODE WHERE B_CD = '" + test_type_cd + "' AND USE_YN = '1' AND CD LIKE '" + cont_gb + "%' AND LENGTH(CD) = 5 GROUP BY SUBSTR(CD, 1, 3)";
					dao = new MasterDao();
					if (LOG_MODE) System.out.print(sql);
					dao.setCommand(sql);
					list = dao.getResultHashtableNoClose();
					dao.close();
					if (list.size() > 0)
					{
						String robust = "", robustCnt = "";
						for (int j=0; j<list.size(); j++)
						{
							dao = new MasterDao();
							robust = ((Hashtable) list.elementAt(j)).get("ROBUST")+"";
							robustCnt = ((Hashtable) list.elementAt(j)).get("CNT")+"";
							// (이전쿼리) sql = "SELECT PARAM_CD FROM TAB_TEST_2_ITEM WHERE CONT_GB = 'A' AND SUBSTR(PARAM_CD, 1, 3) = '" + robust + "' ORDER BY PARAM_CD";
							sql = "SELECT CD PARAM_CD FROM TAB_CODE WHERE B_CD = '" + test_type_cd + "' AND USE_YN = '1' AND SUBSTR(CD, 1, 3) = '" + robust + "' AND LENGTH(CD) = 5 ORDER BY CD";
							dao = new MasterDao();
							if (LOG_MODE) System.out.print(sql);
							dao.setCommand(sql);
							list2 = dao.getResultHashtableNoClose();
							dao.close();
							if (list2.size() > 0)
							{ 
								// 세부파라메터 갯수만큼 Loop 돌면서 대상 콘텐츠 생성
 								for (int k=0; k< list2.size(); k++)
								{
									robust_param = ((Hashtable) list2.elementAt(k)).get("PARAM_CD")+"";
 									int selectCnt = 0;
 									/*
 										robustCnt: 변형파라미터 갯수(해당 평가항목내에 변형파라메터가 몇개까지 있는지 검사.의 쿼리)
 										test_type_cnt: 화면에서 입력한 수량 
 										selectCnt: 해당 평가 항목의 변형파라미터 갯수 나누기 화면에서 입력한 수량  : (int) (Integer.parseInt(test_type_cnt) / genList.size());
 										selectCnt2: 이미지/오디오/비디오 콘텐츠 유형의 장르 갯수 나누기 selectCnt   : (int) (selectCnt / genList.size());
 										row_top: (int) ((double)selectCnt2 * ((double)(100 - 오디오 제거 비디오 비율) / 100.0));
 										genreInsertCnt: 장르별로 루프 돌때마다 selectCnt2을 더함.
 									*/
 									
									if (k == ((list2.size() - 1))){
										// 맨 마지막일때는 전체갯수 맞추기
										selectCnt = Integer.parseInt(test_type_cnt) - ((Integer.parseInt(test_type_cnt) / Integer.parseInt(robustCnt)) * k);
									}else{
										selectCnt = Integer.parseInt(test_type_cnt) / Integer.parseInt(robustCnt);
									}
									
 									//강인성
									if (test_type_cd.equals("X310")){
										int selectCnt2 = 0;
										int genreInsertCnt = 0;
										// 장르별 분포를 고르게 하기
										// TMIS_INFO: 단 이미지일 경우 오디오가 없기때문에 요 로직 안타고 아래 로직에서 저장한다.
										for (int l=0; (l<genList.size() && !cont_gb.equals("I")); l++){
											selectCnt2 = 0;
											double test = 0;
											float cur_persent = 0;
											int row_top = 0;
											String genre = ((Hashtable) genList.elementAt(l)).get("GENRE")+"";
											
											// genre = ((Hashtable) genList.elementAt(l)).get("GENRE")+"";
											// cur_persent = Float.parseFloat(((Hashtable) genList.elementAt(l)).get("CUR_PERCENT")+"");
											selectCnt2 = (int) (selectCnt / genList.size());
											if (selectCnt2 == 0) selectCnt2 = 1;
											
											genreInsertCnt += selectCnt2;

											if (cont_gb.equals("V"))
											{
												row_top = (int) ((double)selectCnt2 * ((double)(100 - Integer.parseInt(audio_remove_rate)) / 100.0));
											}
											//TMIS_INFO: 불필요한 쿼리 call 제거
											if(row_top >= 1){
												// 비디오의 경우 오디오가 포함된 비디오
												sql = "INSERT INTO TAB_TEST_2_PLAN_CONTENTS (TEST_PLAN_ID, CONT_GB, TEST_TYPE_CD, CONTENT_ID, REG_DATE) ";
												sql += "SELECT '" + test_plan_id + "', '" + cont_gb + "', '" + test_type_cd + "', CONTENT_ID, SYSDATE FROM ";
												sql += "	(SELECT ROWNUM, CONTENT_ID FROM PES_GUI_INFO ";
												sql += "		WHERE CONT_TYPE = '2' AND TEST_TYPE_CD = '" + test_type_cd + "' AND USE_YN = 'Y' ";
												sql += "			AND GENRE = '" + genre + "' AND ROBUST_PARAM = '" + robust_param + "' AND A_BITRATE IS NOT NULL ";//아래 쿼리와 다른점은 A_BITRATE IS NOT NULL 이냐 A_BITRATE IS NULL 이냐 차이임.
												sql += "            AND ORG_CONT_ID IN (SELECT CONTENT_ID FROM TAB_TEST_2_PLAN_CONTENTS ";
												sql += "            	WHERE TEST_PLAN_ID = '" + test_plan_id + "' AND CONT_GB = '" + cont_gb + "' AND TEST_TYPE_CD = 'X340') ";
												sql += "		ORDER BY DBMS_RANDOM.VALUE()) ";
												sql += "WHERE ROWNUM <= " + row_top ;
												dao = new MasterDao();
												if (LOG_MODE) System.out.print(sql);
												dao.execute(sql); 
												dao.close();
											}
											// 비디오의 경우 오디오가 제거 비디오
											if (cont_gb.equals("V"))
											{
												row_top = (int) ((double)selectCnt2 * ((double)(Integer.parseInt(audio_remove_rate)) / 100.0));
												
												//TMIS_INFO: 불필요한 쿼리 call 제거
												if(row_top >= 1){
													sql = "INSERT INTO TAB_TEST_2_PLAN_CONTENTS (TEST_PLAN_ID, CONT_GB, TEST_TYPE_CD, CONTENT_ID, REG_DATE) ";
													sql += "SELECT '" + test_plan_id + "', '" + cont_gb + "', '" + test_type_cd + "', CONTENT_ID, SYSDATE FROM ";
													sql += "	(SELECT ROWNUM, CONTENT_ID FROM PES_GUI_INFO ";
													sql += "		WHERE CONT_TYPE = '2' AND TEST_TYPE_CD = '" + test_type_cd + "' AND USE_YN = 'Y' ";
													sql += "			AND GENRE = '" + genre + "' AND ROBUST_PARAM = '" + robust_param + "' AND A_BITRATE IS NULL ";
													sql += "            AND ORG_CONT_ID IN (SELECT CONTENT_ID FROM TAB_TEST_2_PLAN_CONTENTS ";
													sql += "            	WHERE TEST_PLAN_ID = '" + test_plan_id + "' AND CONT_GB = '" + cont_gb + "' AND TEST_TYPE_CD = 'X340') ";
													sql += "		ORDER BY DBMS_RANDOM.VALUE()) ";
													sql += "WHERE ROWNUM <= " + row_top ;
													dao = new MasterDao();
													if (LOG_MODE) System.out.print(sql);
													dao.execute(sql); 
													dao.close();
												}
											}

											if (genreInsertCnt > selectCnt) break;
										}
										
										// 실제 저장된 수량과 비교하여 모자라는 경우 추가 Insert (세부 파라메터별 체크)
										sql = "SELECT COUNT(*) CNT ";
										sql += "FROM TAB_TEST_2_PLAN_CONTENTS A ";
										sql += "INNER JOIN PES_GUI_INFO B ON A.CONTENT_ID = B.CONTENT_ID AND B.TEST_TYPE_CD = A.TEST_TYPE_CD ";
										sql += "WHERE A.TEST_PLAN_ID = '" + test_plan_id + "' ";
										sql += "AND A.CONT_GB = '" + cont_gb + "' ";
										sql += "AND A.TEST_TYPE_CD = '" + test_type_cd + "' ";
										sql += "AND B.ROBUST_PARAM = '" + robust_param + "' ";
										sql += "AND ORG_CONT_ID IN (SELECT CONTENT_ID FROM TAB_TEST_2_PLAN_CONTENTS ";
										sql += "WHERE TEST_PLAN_ID = '" + test_plan_id + "' AND CONT_GB = '" + cont_gb + "' AND TEST_TYPE_CD = 'X340') ";
										
										dao = new MasterDao();
										if (LOG_MODE) System.out.print(sql);
										dao.setCommand(sql);
 										list3 = dao.getResultHashtableNoClose();
										dao.close();
										
										selectCnt2 = Integer.parseInt(((Hashtable) list3.elementAt(0)).get("CNT")+"");
										
 										if (selectCnt2 < selectCnt){
											sql = "INSERT INTO TAB_TEST_2_PLAN_CONTENTS (TEST_PLAN_ID, CONT_GB, TEST_TYPE_CD, CONTENT_ID, REG_DATE) ";
											sql += "SELECT '" + test_plan_id + "', '" + cont_gb + "', '" + test_type_cd + "', CONTENT_ID, SYSDATE FROM ";
											sql += "	(SELECT ROWNUM, CONTENT_ID FROM PES_GUI_INFO ";
											sql += "		WHERE CONT_TYPE = '2' AND TEST_TYPE_CD = '" + test_type_cd + "' AND ROBUST_PARAM = '" + robust_param + "' AND USE_YN = 'Y' ";
											sql += "            AND CONTENT_ID NOT IN (SELECT CONTENT_ID FROM TAB_TEST_2_PLAN_CONTENTS ";
											sql += "                WHERE TEST_PLAN_ID = '" + test_plan_id + "' and CONT_GB = '" + cont_gb + "' AND TEST_TYPE_CD = '" + test_type_cd + "' )";
											sql += "            AND ORG_CONT_ID IN (SELECT CONTENT_ID FROM TAB_TEST_2_PLAN_CONTENTS ";
											sql += "            	WHERE TEST_PLAN_ID = '" + test_plan_id + "' AND CONT_GB = '" + cont_gb + "' AND TEST_TYPE_CD = 'X340') ";
											sql += "		ORDER BY DBMS_RANDOM.VALUE()) ";
											sql += "WHERE ROWNUM <= " + (selectCnt - selectCnt2);
											dao = new MasterDao();
											if (LOG_MODE) System.out.print(sql);
											dao.execute(sql); 
											dao.close();
										}else if (selectCnt < Integer.parseInt(test_type_cnt)){
											// 이미 Insert된 수량이 오버한 경우는..
										}
									}
									//축약성 저장
									else if (test_type_cd.equals("X370"))
									{
										
	 									if(LOG_MODE){
	 										System.out.print("cont_gb: " + cont_gb);
	 										System.out.print("test_type_cd: " + test_type_cd);
	 										System.out.print("robustCnt: " + robustCnt);
	 										System.out.print("test_type_cnt: " + test_type_cnt);
	 										System.out.print("selectCnt: " + selectCnt);
	 									}
	 									
											sql = "INSERT INTO TAB_TEST_2_PLAN_CONTENTS (TEST_PLAN_ID, CONT_GB, TEST_TYPE_CD, CONTENT_ID, REG_DATE) ";
											sql += "SELECT '" + test_plan_id + "', '" + cont_gb + "', '" + test_type_cd + "', CONTENT_ID, SYSDATE FROM ";
											sql += "	(SELECT ROWNUM, CONTENT_ID FROM PES_GUI_INFO ";
											sql += "		WHERE CONT_TYPE = '2' AND TEST_TYPE_CD = '" + test_type_cd + "' AND ROBUST_PARAM = '" + robust_param + "' AND USE_YN = 'Y' ";
											sql += "            AND ORG_CONT_ID IN (SELECT CONTENT_ID FROM TAB_TEST_2_PLAN_CONTENTS ";
											sql += "            	WHERE TEST_PLAN_ID = '" + test_plan_id + "' AND CONT_GB = '" + cont_gb + "' AND TEST_TYPE_CD = 'X340') ";
											sql += "		ORDER BY DBMS_RANDOM.VALUE()) ";
											sql += "WHERE ROWNUM <= " + selectCnt;
											dao = new MasterDao();
											
											if(LOG_MODE)System.out.print(sql);
											System.out.print("축약성:"+cont_gb+"::"+sql);
											
											dao.execute(sql); 
											dao.close();
									}
								}
								// 파라메터항목별 수량 체크(강인성에서만)
								if (test_type_cd.equals("X310"))
								{
									// 실제 저장된 수량과 비교하여 모자라는 경우 추가 Insert
									sql = "SELECT COUNT(*) CNT ";
									sql += "FROM TAB_TEST_2_PLAN_CONTENTS A ";
									sql += "INNER JOIN PES_GUI_INFO B ON A.CONTENT_ID = B.CONTENT_ID AND B.TEST_TYPE_CD = A.TEST_TYPE_CD AND SUBSTR(ROBUST_PARAM, 1, 3) = '" + robust + "' ";
									sql += "WHERE A.TEST_PLAN_ID = '" + test_plan_id + "' ";
									sql += "AND A.CONT_GB = '" + cont_gb + "' ";
									sql += "AND A.TEST_TYPE_CD = '" + test_type_cd + "' ";
									
									dao = new MasterDao();
									if (LOG_MODE) System.out.print(sql);
									dao.setCommand(sql);
									list3 = dao.getResultHashtableNoClose();
									dao.close();
									
									int selectCnt2 = Integer.parseInt(((Hashtable) list3.elementAt(0)).get("CNT")+"");
									
									if (selectCnt2 < Integer.parseInt(test_type_cnt))
									{
										sql = "INSERT INTO TAB_TEST_2_PLAN_CONTENTS (TEST_PLAN_ID, CONT_GB, TEST_TYPE_CD, CONTENT_ID, REG_DATE) ";
										sql += "SELECT '" + test_plan_id + "', '" + cont_gb + "', '" + test_type_cd + "', CONTENT_ID, SYSDATE FROM ";
										sql += "	(SELECT ROWNUM, CONTENT_ID FROM PES_GUI_INFO ";
										sql += "		WHERE CONT_TYPE = '2' AND TEST_TYPE_CD = '" + test_type_cd + "' AND SUBSTR(ROBUST_PARAM, 1, 3) = '" + robust + "' AND USE_YN = 'Y' ";
										sql += "            AND CONTENT_ID NOT IN (SELECT CONTENT_ID FROM TAB_TEST_2_PLAN_CONTENTS ";
										sql += "                WHERE TEST_PLAN_ID = '" + test_plan_id + "' and CONT_GB = '" + cont_gb + "' AND TEST_TYPE_CD = '" + test_type_cd + "' )";
										sql += "            AND ORG_CONT_ID IN (SELECT CONTENT_ID FROM TAB_TEST_2_PLAN_CONTENTS ";
										sql += "            	WHERE TEST_PLAN_ID = '" + test_plan_id + "' AND CONT_GB = '" + cont_gb + "' AND TEST_TYPE_CD = 'X340') ";
										sql += "		ORDER BY DBMS_RANDOM.VALUE()) ";
										sql += "WHERE ROWNUM <= " + (Integer.parseInt(test_type_cnt) - selectCnt2);
										dao = new MasterDao();
										if (LOG_MODE) System.out.print(sql);
										dao.execute(sql); 
										dao.close();
									}
									else if (selectCnt2 < Integer.parseInt(test_type_cnt))
									{
										// 이미 Insert된 수량이 오버한 경우는..
									}
								}
							}
						}
					}
					// 비디오 강인성의 경우 오디오 제거 비율 삭제 후 재 Insert
					if (test_type_cd.equals("X310"))
					{
					}
				}
				
			}
		}
		
	} catch(Exception e) {
 		logger.error(PAGE + " Exception e.toString()      : " + e.toString());
		logger.error(PAGE + " Exception e.getMessage()    : " + e.getMessage());
		logger.error(PAGE + " Exception strQuery  : \n" + sql);
		%>
		<SCRIPT LANGUAGE="JavaScript">
		<!--
			alert(' [<%=PAGE%>] : <%=e.toString().replace("\n","")%>')
			history.back();
		//-->
		</SCRIPT>
		<%
		return;
	} finally {
		//dao.close();
		%>
		 <script>
			alert("저장되었습니다.");
			location.href="testPlanLst.jsp";
		</script>
		<%
	}
 %>
